//
//  HomeViewController.m
//  框架
//
//  Created by 刘明 on 16/3/21.
//  Copyright © 2016年 LM. All rights reserved.
//

#import "HomeViewController.h"
#import "TZUICollectionHeader.h"
//三种CELL
#import "TZBasketBallCell.h"
#import "TZRecommendCell.h"
#import "TONewActivityCell.h"


//二级页面
#import "RecommandViewController.h"
#import "TODetailViewController.h"

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,TZUICollectionHeaderDelegate>
@property(nonatomic,weak)UICollectionView *myCollectionView;
@property(nonatomic,strong)NSArray *array;

@end

@implementation HomeViewController

- (void)viewDidLoad

{
    [super viewDidLoad];
    
    //设置CollectionView
    [self setupCollectionView];
    self.array = [NSArray arrayWithObjects:@"校园争霸",@"中国好声音",@"我不是歌手",nil];
    

}

//创建CollectionView
-(void)setupCollectionView
{
    
    
    //设置CollectionView大小
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    
//    flowLaout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
    //列间距
//    flowLaout.minimumInteritemSpacing = 5;
    //行间距
//    flowLaout.minimumLineSpacing = 5;
    
////    //设置header大小
//    flowLaout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
    flowLaout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 10);
    
    //创建CollecitonView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49-64) collectionViewLayout:flowLaout];
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    self.myCollectionView = collectionView;
    
    //注册三种cell
    [collectionView registerClass:[TZBasketBallCell class] forCellWithReuseIdentifier:@"TZBasketBallCell"];
    [collectionView registerClass:[TZRecommendCell class] forCellWithReuseIdentifier:@"TZRecommendCell"];
    
    [collectionView registerNib:[UINib nibWithNibName:@"TONewActivityCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"TONewActivityCell"];
    
    //注册CollectionView header
    [collectionView registerClass:[TZUICollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TZUICollectionHeader"];
    
    //注册 CollectionView的footer
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionReusableViewFooter"];
    
}

#pragma mark--UICollectionView 的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;

    }
    if (section == 1) {
        return 3;
        
    }
    else
    {
        return 3;
        
    }
   
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0)
    {
        static NSString *BaskBall = @"TZBasketBallCell";
        TZBasketBallCell *BaskCell = [collectionView dequeueReusableCellWithReuseIdentifier:BaskBall forIndexPath:indexPath];
        
        BaskCell.backgroundColor = [UIColor whiteColor];
        
        return BaskCell;
    }
    
    else if (indexPath.section == 1)
        
    {
    static NSString *RecomID = @"TZRecommendCell";
        TZRecommendCell *RecomCell = [collectionView dequeueReusableCellWithReuseIdentifier:RecomID forIndexPath:indexPath];
        RecomCell.backgroundColor = [UIColor whiteColor];
        
      RecomCell.RecommName = self.array[indexPath.row];
       

        
        return RecomCell;
        
    }
    
    else
    {
        
        static NSString *NewID = @"TONewActivityCell";
        TONewActivityCell *NewCell = [collectionView dequeueReusableCellWithReuseIdentifier:NewID forIndexPath:indexPath];
        NewCell.backgroundColor = [UIColor colorWithRed:0.93 green:0.89 blue:0.83 alpha:1.00];
        return NewCell;
        
    }

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
      if (indexPath.section == 0)
      {
          return CGSizeMake(SCREEN_WIDTH, 200);
//        return CGSizeMake((SCREEN_WIDTH - 5*5 - 20)/6,60);
      }
    if (indexPath.section == 1) {
        UICollectionViewFlowLayout *lay = (UICollectionViewFlowLayout*)collectionViewLayout;
        
        lay.sectionInset = UIEdgeInsetsMake(10, 10, 15, 15);

        return CGSizeMake((SCREEN_WIDTH - 60)/3,100);
    }
    else
    {
        UICollectionViewFlowLayout *lay = (UICollectionViewFlowLayout*)collectionViewLayout;
        
        lay.sectionInset = UIEdgeInsetsMake(1, 3, 3, 3);
        return CGSizeMake(SCREEN_WIDTH, 150);
    }
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return CGSizeMake(0, 0);
    }
    else
    {
     return  CGSizeMake(SCREEN_WIDTH, 40);
    }
    

}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        RecommandViewController *recom = [[RecommandViewController alloc]init];
        [self.navigationController pushViewController:recom animated:YES];
    }else{
    
        TODetailViewController *detailVC = [[TODetailViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
  
    


}

//增补视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        TZUICollectionHeader *resHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"TZUICollectionHeader" forIndexPath:indexPath];
        resHeader.collectionHeaderDelegate = self;
        if (indexPath.section == 0) {
    
            resHeader.littleLabel.text = @"校内活动";
        }
        else if (indexPath.section == 1) {
        resHeader.littleLabel.text = @"推荐活动";
        }else
        {
        resHeader.littleLabel.text = @"最新活动";

        }

        return resHeader;
        
    }
    else
  {
       UICollectionReusableView *resFooter = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableViewFooter" forIndexPath:indexPath];
      resFooter.backgroundColor = [UIColor colorWithRed:0.52 green:0.58 blue:0.62 alpha:1];
             return resFooter;
    
    }

}

-(void)moreBtnAction{
    
    RecommandViewController *recommandVC = [[RecommandViewController alloc] init];
    [self.navigationController pushViewController:recommandVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
